﻿// Spun AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spun.Naming;
using Spun.Naming.Paths;
using Spun.Utility.Locking;
using Spun.Models;
using Spun.Bindings.Observation;

namespace Spun.Bindings
{
    public class BindingContext : IEnumerable<IBinding>
    {
        public BindingContext()
        {
            Bindings = new Dictionary<Path, IBinding>();

            lockingContext = new LockingContext();
            namingContext = new NamingContext(LockingContext);
        }

        public NamingContext NamingContext
        {
            get { return namingContext; }
        }

        public LockingContext LockingContext
        {
            get { return lockingContext; }
        }

        public IBinding this[Path Path]
        {
            get
            {
                using (LockingContext.ReadLock)
                    return Bindings[Path];
            }
        }

        public void AddBinding(IBinding Binding)
        {
            if (Invalid(Binding))
                Binding.Invalidated();

            using (LockingContext.WriteLock)
            {
                Bindings.Add(Binding.Path, Binding);

                new BindingObserver(Binding, this);
            }

            Binding.Bound(this);
        }

        public void RemoveBinding(IBinding Binding)
        {
            using (LockingContext.WriteLock)
                Bindings.Remove(Binding.Path);
        }

        public IEnumerable<IBinding> PrefixesFor(Path Path)
        {
            using (LockingContext.ReadLock)
            {
                return from Binding in Bindings.Values
                       where Binding.Path.IsPrefix(Path)
                       select Binding;
            }
        }

        public IEnumerable<IBinding> PrefixesOf(Path Path)
        {
            using (LockingContext.ReadLock)
            {
                return from Binding in Bindings.Values
                       where Path.IsPrefix(Binding.Path)
                       select Binding;
            }
        }

        private bool Invalid(IBinding Binding)
        {
            try
            {
                object O = new PathResolver(NamingContext).Resolve(Binding.Path);
                return (O == null);
            }
            catch (CouldNotResolvePath)
            {
                return true;
            }
        }

        private ModelBase GetValue(IEnumerable<IPathNode> AncestorPathNodes)
        {
            try
            {
                Path AncestorPath = new Path(AncestorPathNodes);

                object Ancestor = new PathResolver(NamingContext).Resolve(AncestorPath);

                if (Ancestor == null)
                    return null;

                ModelBase Model = Ancestor as ModelBase;

                if (Model == null)
                    throw new ObjectInModelChainNotAModelBase(AncestorPath, Ancestor);

                return Model;
            }
            catch (CouldNotResolvePath)
            {
                return null;
            }
        }

        private LockingContext lockingContext;
        private NamingContext namingContext;
        private IDictionary<Path, IBinding> Bindings;

        #region IEnumerable<IBinding> Members

        public IEnumerator<IBinding> GetEnumerator()
        {
            return Bindings.Values.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }
}
